//
//  UsedCarTableViewController.m
//  Car
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "UsedCarTableViewController.h"
#import "UsedCarModel.h"
#import "UsedCarTableViewCell.h"
#import "MJRefresh.h"
#import "CarDetailViewController.h"
#import "CarFindHistory.h"
@interface UsedCarTableViewController ()
@property (nonatomic ,strong)NSMutableArray *usedCarArray;
@end

@implementation UsedCarTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [ProgressHUD show:@"正在加载..."];
    [self setUpData];
    [self reloadAndJosnWith:kUsedCar];
}

- (void)reloadAndJosnWith:(NSString *)url{
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *Dic in dic[@"result"][@"list"]) {
            UsedCarModel *model = [UsedCarModel JosnWithDictionary:Dic];
            [self.usedCarArray addObject:model];
        }
        [self.tableView reloadData];
        [ProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)setUpData{
    __block UsedCarTableViewController *mark = self;
    __block int a = 1;
    [self.tableView addFooterWithCallback:^{
        a++;
        NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/2sc/2scs-pm2-pid0-c0-b0-ss0-sp0-l0-o0-minp0-maxp0-minm0-maxm0-mina0-maxa0-f0-p%d-s30.json",a];
        [mark reloadAndJosnWith:url];
        [mark.tableView footerEndRefreshing];
    }];
    
    
    
    [self.tableView registerClass:[UsedCarTableViewCell class] forCellReuseIdentifier:@"cell"];
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc]initWithTitle:@"品牌" style:(UIBarButtonItemStyleDone) target:self action:@selector(aButtonClick:)];
    UIBarButtonItem *bButton = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(bButtonClick:)];
    UIBarButtonItem *cButton = [[UIBarButtonItem alloc]initWithTitle:@"降价" style:(UIBarButtonItemStyleDone) target:self action:@selector(cButtonClick:)];
    UIBarButtonItem *dButton = [[UIBarButtonItem alloc]initWithTitle:@"二手车" style:(UIBarButtonItemStyleDone) target:self action:@selector(dButtonClick:)];
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:aButton,bButton,cButton,dButton, nil];
    self.navigationItem.leftBarButtonItems = buttonArray;
}
- (NSMutableArray *)usedCarArray{
    if (!_usedCarArray ) {
        _usedCarArray = [[NSMutableArray alloc]init];
    }
    return _usedCarArray;
}
- (void)aButtonClick:(UIBarButtonItem *)button{
    FindTableViewController *findVC = [[FindTableViewController alloc]init];
    SaleTableViewController *saleVC = [[SaleTableViewController alloc]init];
    IIViewDeckController *deckVC = [[IIViewDeckController alloc]initWithCenterViewController:findVC leftViewController:saleVC];
    deckVC.leftSize = 75;
    [self.navigationController pushViewController:deckVC animated:NO];
    
    
}
- (void)bButtonClick:(UIBarButtonItem *)button{
    ScreenTableViewController *screenVC = [[ScreenTableViewController alloc]init];
    [self.navigationController pushViewController:screenVC animated:NO];
}
- (void)cButtonClick:(UIBarButtonItem *)button{
    MarkDownTableViewController *markDownVC = [[MarkDownTableViewController alloc]init];
    [self.navigationController pushViewController:markDownVC animated:NO];
}
- (void)dButtonClick:(UIBarButtonItem *)button{
    UsedCarTableViewController *usedCarVC = [[UsedCarTableViewController alloc]init];
    [self.navigationController pushViewController:usedCarVC animated:NO];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.usedCarArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UsedCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell setWithModel:self.usedCarArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CarDetailViewController *detailView = [[CarDetailViewController alloc] init];
    detailView.url = [self.usedCarArray[indexPath.row] ID];
	UsedCarModel *model = self.usedCarArray[indexPath.row];
	[[CarFindHistory shareHistory] addCarFindHistoryModel:model WithUser:[EnterInformationHandle share].myNumber];
	
    detailView.num = 1;
    [self.navigationController pushViewController:detailView animated:YES];
}


@end
